﻿using System;
using System.Collections.Generic;
using Gallio.Framework;
using MbUnit.Framework;
using TSTIntegration;
using TSTIntegration.ADO;
/* ***************************************************** *
 * Developed by Philip Japikse, www.japikse.blogspot.com *
 * see License.txt for license information               *
 * ***************************************************** */

namespace TSTIntegrationTests.ADO
{
    [TestFixture]
    public class SQLTestHarnessTests
    {
        [Test]
        public void ShouldExecuteAllTestsAndClearAllData()
        {
            int sessionID;
            var harness = createSUT();
            var resultFlag = harness.ExecuteSQLAndReturnResult(string.Empty, out sessionID);
            Assert.IsTrue(resultFlag);
            Assert.AreEqual(34,harness.GetResultData(sessionID).Rows.Count);
            harness.CleanTestResults(sessionID);
            Assert.AreEqual(0, harness.GetResultData(sessionID).Rows.Count);
        }

        [Test]
        public void ShouldGetTableFromTSTResults()
        {
            int sessionID;
            var harness = createSUT();
            harness.ExecuteSQLAndReturnResult(string.Empty, out sessionID);
            var dtResults = harness.GetResultData(sessionID);
            harness.CleanTestResults(sessionID);
            Assert.AreEqual(34, dtResults.Rows.Count);
        }

        [Test]
        public void ShouldConvertResultsToTestDataList()
        {
            int sessionID;
            var harness = createSUT();
            harness.ExecuteSQLAndReturnResult(string.Empty, out sessionID);
            var dtResults = harness.GetResultData(sessionID);
            harness.CleanTestResults(sessionID);
            var testResults =SQLTestHarness.ConvertTableToTestResults(dtResults);
            Assert.AreEqual(34, testResults.Count);
        }
        [Test]
        public void ShouldGetSuitesFromListOfTestData()
        {
            var results = new List<TestResultData>
                          {
                              new TestResultData
                              {
                                  AssertPassed = true,
                                  CreatedTime = DateTime.Now,
                                  LogMessage = "Message",
                                  StoredProcName = "Proc1",
                                  StoredProcPassed = true,
                                  SuiteID = 5,
                                  SuiteName = "Suite1",
                                  SuitePassed = true,
                                  TestSessionID = 6
                              },
                              new TestResultData
                              {
                                  AssertPassed = true,
                                  CreatedTime = DateTime.Now,
                                  LogMessage = "Message",
                                  StoredProcName = "Proc2",
                                  StoredProcPassed = true,
                                  SuiteID = 5,
                                  SuiteName = "Suite2",
                                  SuitePassed = true,
                                  TestSessionID = 6
                              },
                              new TestResultData
                              {
                                  AssertPassed = true,
                                  CreatedTime = DateTime.Now,
                                  LogMessage = "Message",
                                  StoredProcName = "Proc3",
                                  StoredProcPassed = true,
                                  SuiteID = 5,
                                  SuiteName = "Suite2",
                                  SuitePassed = true,
                                  TestSessionID = 6
                              }
                          };
            Assert.AreEqual(2, SQLTestHarness.GetDistinctTestSuites(results).Count);
        }
        [Test]
        public void ShouldGetSprocsFromListOfTestData()
        {
            var results = new List<TestResultData>
                          {
                              new TestResultData
                              {
                                  AssertPassed = true,
                                  CreatedTime = DateTime.Now,
                                  LogMessage = "Message",
                                  StoredProcName = "Proc1",
                                  StoredProcPassed = true,
                                  SuiteID = 5,
                                  SuiteName = "Suite1",
                                  SuitePassed = true,
                                  TestSessionID = 6
                              },
                              new TestResultData
                              {
                                  AssertPassed = true,
                                  CreatedTime = DateTime.Now,
                                  LogMessage = "Message",
                                  StoredProcName = "Proc2",
                                  StoredProcPassed = true,
                                  SuiteID = 5,
                                  SuiteName = "Suite2",
                                  SuitePassed = true,
                                  TestSessionID = 6
                              },
                              new TestResultData
                              {
                                  AssertPassed = true,
                                  CreatedTime = DateTime.Now,
                                  LogMessage = "Message",
                                  StoredProcName = "Proc2",
                                  StoredProcPassed = true,
                                  SuiteID = 5,
                                  SuiteName = "Suite2",
                                  SuitePassed = true,
                                  TestSessionID = 6
                              },
                              new TestResultData
                              {
                                  AssertPassed = true,
                                  CreatedTime = DateTime.Now,
                                  LogMessage = "Message",
                                  StoredProcName = "Proc3",
                                  StoredProcPassed = true,
                                  SuiteID = 5,
                                  SuiteName = "Suite2",
                                  SuitePassed = true,
                                  TestSessionID = 6
                              }
                          };
            Assert.AreEqual(2, SQLTestHarness.GetDistinctSprocsPerSuite(results,"Suite2").Count);
        }

        [Test]
        public void ShouldConvertResultsToListOfTestDataWithPublicAPI()
        {
            Assert.AreEqual(34, createSUT().RunSQLTestsReturningTestResultData(string.Empty).Count);
        }

        [DynamicTestFactory]
        public IEnumerable<Test> ShouldGetGroupedResultsFromSQLTest()
        {
            return createSUT().RunSQLTestsReturningTestCases(string.Empty, TestContext.CurrentContext);
        }
        private static SQLTestHarness createSUT()
        {
            return new SQLTestHarness("TSTConnectionString", "ConnectionString");
        }


    }
}